//
//  QGRegistViewController.m
//  qeegooChat
//
//  Created by heew on 16/1/7.
//  Copyright © 2016年 com.autozi. All rights reserved.
//

#import "QGRegistViewController.h"
#import "QGRegistInputTextCell.h"

@interface QGRegistViewController () <UITableViewDataSource,UITableViewDelegate,QGRegistInputTextCellDelegate>

@property (nonatomic,strong)UITableView *listTable;

@property (nonatomic,strong)NSMutableArray *sourceArray;

@end

@implementation QGRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.navigationController.navigationBar setHidden:NO];
    [self setStrNavTitle:@"注册新用户"];
    [self setRightButtonWithTitle:@"发送"];
    
    self.view.backgroundColor = QGMainBackgroundColor;
    
    self.sourceArray = [[NSMutableArray alloc]init];
    
    QGRegistContentModel *nameItem = [[QGRegistContentModel alloc]init];
    nameItem.tagName = @"账   号";
    nameItem.placeHolder = @"请输入用于登录的唯一用户名，可以是汉字";
    nameItem.contentType = HARegistContentTypeUserName;
    nameItem.contentHeight = 64.f;
    
    [self.sourceArray addObject:nameItem];
    
    QGRegistContentModel *passwordItem = [[QGRegistContentModel alloc]init];
    passwordItem.tagName = @"密   码";
    passwordItem.placeHolder = @"请输入用于登录的密码";
    passwordItem.contentType = HARegistContentTypePassword;
    passwordItem.contentHeight = 64.f;
    
    [self.sourceArray addObject:passwordItem];
    
    self.listTable = [[UITableView alloc]init];
    self.listTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.listTable.viewHeight = SCREEN_HEIGHT - 20;
    self.listTable.viewTop = 20;
    self.listTable.viewWidth = SCREEN_WIDTH;
    self.listTable.backgroundColor = QGMainBackgroundColor;
    self.listTable.dataSource = self;
    self.listTable.delegate = self;
    [self.view addSubview:self.listTable];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    
    QGRegistInputTextCell *cell = (QGRegistInputTextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell = [[QGRegistInputTextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.delegate = self;
    }
    [cell setContentModel:[self.sourceArray objectAtIndex:indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QGRegistContentModel *contentModel = [self.sourceArray objectAtIndex:indexPath.row];
    
    return contentModel.contentHeight;
}

- (void)inputCell:(QGRegistInputTextCell *)inputCell didUpdateContent:(NSString *)content
{
    NSIndexPath *indexPath = [self.listTable indexPathForCell:inputCell];
    
    QGRegistContentModel *contentModel = [self.sourceArray objectAtIndex:indexPath.row];
    contentModel.content = content;
    
    [self.sourceArray replaceObjectAtIndex:indexPath.row withObject:contentModel];
    
}

- (void)rightButtonPressed:(UIButton *)sender
{
    QGRegistContentModel *userNameItem = [self.sourceArray firstObject];
    
    if ([QGUitility stringIsNull:userNameItem.content]) {
        [self showErrorMessage:@"登陆手机号不为空"];
        return;
    }
    
    QGRegistContentModel *passwordItem = [self.sourceArray objectAtIndex:1];
    
    if ([QGUitility stringIsNull:passwordItem.content]) {
        [self showErrorMessage:@"密码不为空"];
        return;
    }
    
    [SVProgressHUD showInfoWithStatus:@"正在注册..."];
    
    QGWeakObject(self) weakSelf = self;
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:userNameItem.content password:passwordItem.content withCompletion:^(NSString *username, NSString *password, EMError *error) {
        
        [SVProgressHUD dismiss];
        
        if (!error) {
            
            [SVProgressHUD showSuccessWithStatus:@"注册成功"];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [self showErrorMessage:error.description];
        }
        
    } onQueue:nil];
}

@end
